package edu.ccut.saturn.studio.authority.page;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.TowComponentSearchInProjectDialog;
import edu.ccut.saturn.studio.core.search.dialog.TwoCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;

/**
 * 
 * 
 * 
 */
public class AddResourceDialog extends TitleAreaDialog {


	private Shell shell;

	private Text descriptionText;
	private Text valueText;
	private Text nameText;

	private String name;
	private String value;
	private String descritpion;

	private IProject project = null;

	/**
	 * Create the dialog
	 * 
	 * @param parentShell
	 */
	public AddResourceDialog(Shell parentShell, IProject project) {
		super(parentShell);
		this.shell = parentShell;
		this.project = project;
	}

	/**
	 * Create contents of the dialog
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.marginTop = 5;
		gridLayout.marginRight = 5;
		gridLayout.marginLeft = 5;
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

		final Label nameLabel = new Label(container, SWT.NONE);
		nameLabel.setText(Messages.getString("AddResourceDialog.name")); //$NON-NLS-1$

		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 2, 1);
		nameText.setLayoutData(gd_nameText);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				dialogChanged();
			}
		});
		
		

		final Label valueLabel = new Label(container, SWT.NONE);
		valueLabel.setText(Messages.getString("AddResourceDialog.value")); //$NON-NLS-1$

		valueText = new Text(container, SWT.BORDER);
		valueText.setTextLimit(100);
		final GridData gd_valueText = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		valueText.setLayoutData(gd_valueText);
		valueText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				dialogChanged();
			}
		});
		
		

		final Label browser = new Label(container, SWT.NONE);
		browser.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS)); //$NON-NLS-1$
				
						if (project != null) {
							String  suffixes [] = {"action","jsp"};
							TowComponentSearchInProjectDialog dialog1 = new TowComponentSearchInProjectDialog(
									shell , suffixes,project);
							dialog1.setTitle(Messages.getString("AddResourceDialog.5")); //$NON-NLS-1$
							if (dialog1.open() == Window.OK) {
								SearchResult result = (SearchResult) dialog1
										.getFirstResult();
								String filePath = result.getFile() + ""; //$NON-NLS-1$
								if ((result.getFile()+"").endsWith(".jar")) {
									
									Node RootNode = result.getDocument().getDocumentElement();
									Node InformationNode = SaturnW3CUtil.getFirstChildByName(RootNode, "information");
									String key = SaturnW3CUtil.getChildNodeValue(InformationNode, "key");
									valueText.setText(result.getBundleName()+ "::" + key);
								} else{
									
									String tempPath = filePath.replace(
											result.getSrcName() + "",
											"");
									valueText.setText(tempPath.replace(
											"\\", "/"));
								}
					}
			}}
		});
		final GridData gd_browser = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false);
		browser.setLayoutData(gd_browser);
		browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));

		browser.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});

//		browser.addMouseListener(new MouseAdapter() {
//			public void mouseDown(final MouseEvent e) {
//				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS)); //$NON-NLS-1$
//
//				if (project != null) {
//					String  suffixes [] = {"action","jsp"};
//					TwoCompontentSearchDialog dialog1 = new TwoCompontentSearchDialog(
//							shell , suffixes);
//					dialog1.setTitle(Messages.getString("AddResourceDialog.5")); //$NON-NLS-1$
//					if (dialog1.open() == Window.OK) {
//						SearchResult result = (SearchResult) dialog1
//								.getFirstResult();
//						String filePath = result.getFile() + Messages.getString("AddResourceDialog.4"); //$NON-NLS-1$
//						String tempPath = filePath.replace(result.getSrcName()
//								+ Messages.getString("AddResourceDialog.3"), Messages.getString("AddResourceDialog.2")); //$NON-NLS-1$ //$NON-NLS-2$
//						valueText.setText(tempPath.replace(Messages.getString("AddResourceDialog.1"), Messages.getString("AddResourceDialog.0"))); //$NON-NLS-1$ //$NON-NLS-2$
//
//						// (result.getFileName().endsWith("action")) {
//						// Node InformationNode = SaturnW3CUtil
//						// .getFirstChildByName(result.getDocument()
//						// .getDocumentElement(),
//						// "information");
//						// Node KeyNode = SaturnW3CUtil.getFirstChildByName(
//						// InformationNode, "key");
//						// String keyValue = SaturnW3CUtil
//						// .getNodeValue(KeyNode);
//						// valueText.setText(keyValue);
//
//					}
//
//				}
//			}
//		});

		final Label descritpionLabel = new Label(container, SWT.NONE);
		descritpionLabel.setText(Messages.getString("AddResourceDialog.6"));  //$NON-NLS-1$
		descriptionText = new Text(container, SWT.BORDER);
		descriptionText.setTextLimit(200);
		descriptionText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				dialogChanged();
			}
		});
		final GridData gd_descriptionText = new GridData(SWT.FILL, SWT.CENTER,
				true, false, 2, 1);
		descriptionText.setLayoutData(gd_descriptionText);

		setTitle(Messages.getString("AddResourceDialog.7"));  //$NON-NLS-1$
		setMessage(Messages.getString("AddResourceDialog.8"));  //$NON-NLS-1$
//		dialogChanged();
		return area;
	}

//	private List<String> formatPath(List<String> oldList) {
//		List<String> newList = new ArrayList<String>();
//		for (int i = 0; i < oldList.size(); i++) {
//			String str = oldList.get(i);
//			str = str.substring(str.indexOf(project.getName()));
//			String[] aaa = str.split("\\\\"); //$NON-NLS-1$
//			str = "/" + str.substring(str.indexOf(aaa[2])).replace("\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
//			newList.add(str);
//		}
//		return newList;
//	}
//
//	private String[] formatArray(String[] array, String extendName) {
//		String[] newArray = new String[array.length];
//		for (int i = 0; i < array.length; i++) {
//			String str = array[i];
//			String extend = str.substring(str.lastIndexOf(".") + 1); //$NON-NLS-1$
//			if (extendName.equals(extend)) {
//				newArray[i] = str;
//			}
//		}
//		return newArray;
//	}

	/**
	 * Create contents of the button bar
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, Messages.getString("AddResourceDialog.9"), true);  //$NON-NLS-1$
		createButton(parent, IDialogConstants.CANCEL_ID, Messages.getString("AddResourceDialog.10"), false);  //$NON-NLS-1$
		dialogChanged();
		
	}

	private void dialogChanged() {

		if (SaturnValidation.isEmpty(nameText.getText())) {
			setErrorMessage((Messages.getString("AddResourceDialog.11")));  //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}

		if (SaturnValidation.isBadWord(nameText.getText())) {
			setErrorMessage(Messages.getString("AddResourceDialog.12"));  //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}
		if (SaturnValidation.isEmpty(valueText.getText())) {
			setErrorMessage(Messages.getString("AddResourceDialog.13"));  //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}
		setErrorMessage(null);
		this.getButton(IDialogConstants.OK_ID).setEnabled(true);
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(500, 375);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("AddResourceDialog.14"));  //$NON-NLS-1$
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			name = nameText.getText();
			value = valueText.getText();
			descritpion = descriptionText.getText();
		}
		super.buttonPressed(buttonId);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getDescritpion() {
		return descritpion;
	}

	public void setDescritpion(String descritpion) {
		this.descritpion = descritpion;
	}

}
